package org.epidroid.epidroid.chronos.ui;

import java.util.ArrayList;
import java.util.Iterator;

import org.epidroid.epidroid.R;
import org.epidroid.epidroid.chronos.engine.Chronos;
import org.epidroid.epidroid.chronos.engine.objects.ChronosNode;
import org.epidroid.epidroid.chronos.service.ChronosService;
import org.epidroid.epidroid.chronos.service.ChronosServiceBinder;
import org.epidroid.epidroid.chronos.service.IChronosService;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.ListView;

public class ChronosAllGroupsActivity extends Activity {

	private IChronosService _service = null;

	private static String[] _menus = { "trainnees", "rooms", "professors" };
	private ChronosGroupsAdapter _adapter = null;
	private ChronosGroupsNavAdapter _navAdapter = null;
	private ListView _list = null;
	private ListView _navList = null;
	private ArrayList<ChronosNode> _navItems = null;
	private ProgressDialog _dialog = null;
	private ServiceConnection _serviceConnection = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		bindWithChronosService();
	}

	@Override
	protected void onDestroy() {
		//unbindService(_serviceConnection);
	}

	@Override
	protected void onResume() {
		if (null != _adapter)
			_adapter.notifyDataSetChanged();
		super.onResume();
	}

	private void bindWithChronosService() {
		Intent intent = new Intent(this, ChronosService.class);

		_serviceConnection = new ServiceConnection() {
			public void onServiceConnected(ComponentName name,
					IBinder servicebinder) {
				IChronosService service = ((ChronosServiceBinder) servicebinder)
						.getService();
				_service = service;
				init();
			}

			public void onServiceDisconnected(ComponentName name) {
				Log.i(Chronos.LOGCAT, "Service has disconnected!");
			}
		};

		if (!getApplicationContext().bindService(intent, _serviceConnection,
				Context.BIND_AUTO_CREATE)) {
			Log.i(Chronos.LOGCAT, "Could not bind with service");
		}
	}

	private void init() {
		setContentView(R.layout.chronos_groups);

		_list = (ListView) findViewById(R.id.chronos_groups_list);
		_adapter = new ChronosGroupsAdapter(this, R.layout.chronos_group_item,
				_service);
		_list.setAdapter(_adapter);
		_list.setDivider(null); // TODO: do that in xml layout
		_adapter.notifyDataSetChanged();

		_navItems = new ArrayList<ChronosNode>();
		_navList = (ListView) findViewById(R.id.chronos_groups_nav);
		_navAdapter = new ChronosGroupsNavAdapter(this,
				R.layout.chronos_group_nav_item, _navItems);
		_navList.setAdapter(_navAdapter);
		_navAdapter.notifyDataSetChanged();

		displayNode("trainnees");
	}

	private void showProgressDialog() {
		_dialog = ProgressDialog.show(this, "", "Loading...", true);
	}

	private void hideProgressDialog() {
		if (null == _dialog)
			return;
		_dialog.cancel();
	}

	public void displayNode(final String id) {
		showProgressDialog();
		Runnable r = new Runnable() {
			@Override
			public void run() {
				final ChronosNode n = _service.getMenu(id);
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						displayNode(n);
						ChronosAllGroupsActivity.this.hideProgressDialog();
					}
				});
			}
		};
		Thread t = new Thread(r);
		t.start();
	}

	public void displayNode(ChronosNode node) {
		updateNav(node);
		_adapter.setList(node.getNodes());
	}

	private void updateNav(ChronosNode node) {
		// Find the node in the nav list and delete everything after it
		int i = 0;
		boolean found = false;
		Iterator<ChronosNode> it = _navItems.iterator();

		while (it.hasNext()) {
			ChronosNode current = it.next();
			if (current == node)
				found = true;
			if (found) {
				it.remove();
			}
		}

		pushNavItem(node);
		_navAdapter.notifyDataSetChanged();
	}

	private void pushNavItem(ChronosNode node) {
		_navItems.add(node);
		_navAdapter.notifyDataSetChanged();
		System.out.println("added " + node.getName());
	}

	public void openGroup(ChronosNode node, int type) {
		Intent intent = new Intent(this, ChronosViewActivity.class);
		intent.putExtra("group", node.getName());
		intent.putExtra("type", type);
		startActivity(intent);
	}

}
